package com.sr.srinterpolation.util;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageUtil {
    private static final String TAG = "ImageUtil";

    /**
     * 保存图片
     */
    public static void saveImage(View view) {
        // 从view中截取bitmap
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        bitmap.eraseColor(Color.WHITE);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);

        // 保存图片到手机中
        File file = new File(view.getContext().getCacheDir(), "image.jpg");
//        File file = new File(view.getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES), "image.jpg");
        FileOutputStream out = null;
        try {
            Log.i(TAG, "saveImage: " + file.getAbsolutePath());
            Log.i(TAG, "getExternalStorageDirectory: " + Environment.getExternalStorageDirectory().getPath());
            out = new FileOutputStream(file);
            boolean compress = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            if (compress) {
                // 提示相册更新
//                Uri updateUri = Uri.fromFile(file);
//                Uri updateUri = Uri.parse("file://" + file.getPath());
//                Intent updateIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, updateUri);
//                view.getContext().sendBroadcast(updateIntent);
                // 提示相册更新
//                MediaScannerConnection.scanFile(
//                        view.getContext(),
//                        new String[]{file.getAbsolutePath()},
//                        new String[]{"image/jpeg"},
//                        new MediaScannerConnection.MediaScannerConnectionClient() {
//                            @Override
//                            public void onMediaScannerConnected() {
//                                Log.i(TAG, "onMediaScannerConnected: ");
//                            }
//
//                            @Override
//                            public void onScanCompleted(String path, Uri uri) {
//                                Log.i(TAG, "onScanCompleted: path: " + path + ", uri: " + uri);
//                            }
//                        }
//                );
                // 保存图片到剪切板
                copyImageToClipboard(view.getContext(), file);
                Log.i(TAG, "saveImage: success");
                Toast.makeText(view.getContext(), "保存成功", Toast.LENGTH_SHORT).show();
            } else {
                Log.i(TAG, "saveImage: 压缩失败");
                Toast.makeText(view.getContext(), "压缩失败", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.i(TAG, "saveImage: failure");
            Toast.makeText(view.getContext(), "保存失败，" + e.getMessage(), Toast.LENGTH_SHORT).show();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 保存图片到剪切板
     */
    public static void copyImageToClipboard(Context context, File file) {
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard != null) {
            Uri uri = Uri.parse("content://com.sr.srinterpolation.FileProvider" + file.getAbsolutePath());
            ClipData clipData = ClipData.newUri(context.getContentResolver(), "Image", uri);
            clipboard.setPrimaryClip(clipData);
        }
    }
}
